<?php

class UsersController extends AdminAppController {
    public $layout = "default";
  public $paginate = array(
        'limit' => 25,
        'conditions' => array('status' => '1'),
        'order' => array('User.username' => 'asc' ) 
    );
  
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('login','add'); 
    }
  


  public function login() {
    $this->layout = false;
    //if already logged-in, redirect
    if($this->Session->check('Auth.User')){
      $this->redirect(array('action' => 'index'));    
    }
    
    // if we get the post information, try to authenticate
    if ($this->request->is('post')) {
      if ($this->Auth->login()) {
        $this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
        $this->redirect($this->Auth->redirectUrl());
      } else {
            $this->Session->setFlash(
                                'Sai tên đăng nhập hoặc mật khẩu',
                                'default',
                                array('class' => 'alert alert-danger')
                            ); 
      }
    } 
  }

  public function logout() {
    $this->redirect($this->Auth->logout());
  }

    public function index() {
        $this->loadModel('Admin.User');
        $user = $this->User->get_user();

        $list_user = array();
        foreach ($user as $u) {
            $temp = array();
            $temp['user_id'] = $u['users']['id'];
            $temp['name'] = $u['users']['name'];
            $temp['username'] = $u['users']['username'];
            $temp['email'] = $u['users']['email'];
            $temp['created'] = $u['users']['created'];
            $temp['status'] = $u['users']['status'];
            $temp['gender'] = $u['users']['gender'];
            $temp['phone'] = $u['users']['phone'];
            $list_user[] = $temp;
        }
        $this->set('list_user', $list_user);
    }


    public function add() {
        if ($this->request->is('post')) {
          $this->User->create();
          if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(
                                'Thêm mới người dùng thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array('action' => 'index'));
          } else {
            
          }
        }
    }

    public function edit($user_id = "")
    {
        if($user_id == ""){
            $this->redirect(array("controller" => "users", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('user_id', $user_id);
        $user_info = $this->User->findById($user_id);
        $this->set('user_info', $user_info['User']);

        if($this->request->isPost())
        {
            $this->User->id = $user_id;
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(
                                'Sửa thông tin người dùng thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
                $this->redirect(array('action' => 'index'));
            }else{
                $this->Session->setFlash(('Unable to update your user.'));
            }
        }
    }
    public function edit_bk($id = null) {

        if (!$id) {
        $this->Session->setFlash('Please provide a user id');
        $this->redirect(array('action'=>'index'));
      }

      $user = $this->User->findById($id);
      if (!$user) {
        $this->Session->setFlash('Invalid User ID Provided');
        $this->redirect(array('action'=>'index'));
      }

      if ($this->request->is('post') || $this->request->is('put')) {
        $this->User->id = $id;
        if ($this->User->save($this->request->data)) {
          $this->Session->setFlash(__('The user has been updated'));
          $this->redirect(array('action' => 'edit', $id));
        }else{
          $this->Session->setFlash(__('Unable to update your user.'));
        }
      }

      if (!$this->request->data) {
        $this->request->data = $user;
      }
    }

    public function delete($id = null) {
    
    if (!$id) {
      $this->Session->setFlash(
                                'Không tìm thấy người dùng cần xóa',
                                'default',
                                array('class' => 'alert alert-success')
                            );
      $this->redirect(array('action'=>'index'));
    }
    
        $this->User->id = $id;
        if (!$this->User->exists()) {
            $this->Session->setFlash(
                                'Không tìm thấy người dùng cần xóa',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array('action'=>'index'));
        }
        if ($this->User->saveField('status', 0)) {
            $this->Session->setFlash(
                                'Xóa người dùng thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(
                                'Xóa người dùng không thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
        $this->redirect(array('action' => 'index'));
    }
  
  public function activate($id = null) {
    
    if (!$id) {
      $this->Session->setFlash('Please provide a user id');
      $this->redirect(array('action'=>'index'));
    }
    
        $this->User->id = $id;
        if (!$this->User->exists()) {
            $this->Session->setFlash('Invalid user id provided');
      $this->redirect(array('action'=>'index'));
        }
        if ($this->User->saveField('status', 1)) {
            $this->Session->setFlash(__('User re-activated'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not re-activated'));
        $this->redirect(array('action' => 'index'));
    }

}

?>